﻿Imports TodoMicroSolutionPackV1.facturacion.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1.dal.dto
Imports TodoMicroSolutionPackV1.bl
Imports TodoMicroSolutionPackV1.bl.bo
Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Namespace facturacion.bl.bo
    Public Class comprobanteBO
        Inherits genericNhiberateBusinessObject(Of Integer, comprobante)
        Private _comprobante As comprobante
        Private _actualizarStockAutomatico As Boolean = True

        Public Sub New(ByRef daoFactory As NhibernateDAOFactory, ByRef dto As comprobante)
            MyBase.New(daoFactory, dto)
            _comprobante = DirectCast(dto, comprobante)
        End Sub


#Region "Rules"

        Public Overrides Function createRules() As System.Collections.Generic.List(Of Rule)
            Dim rules As List(Of Rule) = MyBase.createRules
            rules.Add(New SimpleRule("emisor", "No hay un emisor seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.emisor Is Nothing)))
            rules.Add(New SimpleRule("receptor", "No hay un receptor seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.receptor Is Nothing)))
            rules.Add(New SimpleRule("tipo_comprobante", "No hay un tipo de comprobante seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.tipo_comprobante Is Nothing)))
            rules.Add(New SimpleRule("tipo_comprobante", "La constante asignada al tipo de comprobante no corresponde a un tipo de comprobante", AddressOf tipoComprobanteConstantTypeIsOK))
            rules.Add(New SimpleRule("punto_venta", "No hay un punto de venta seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.punto_venta Is Nothing)))
            rules.Add(New SimpleRule("numero_comprobante", "El formato del nuero de comprobante es invalido", Function() (_comprobante Is Nothing) OrElse (_comprobante.numero_comprobante > 0)))
            rules.Add(New SimpleRule("concepto", "No hay un concepto seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.concepto Is Nothing)))
            rules.Add(New SimpleRule("concepto", "La constante asignada al concepto no corresponde a un tipo de concepto", AddressOf conceptoConstantTypeIsOK))
            rules.Add(New SimpleRule("importe_total_no_gravados", "El importe total de conceptos no gravados debe ser un numero positivo", Function() (_comprobante Is Nothing) OrElse (_comprobante.importe_total_no_gravados >= 0)))
            rules.Add(New SimpleRule("importe_neto", "El importe neto debe ser mayor a cero", Function() (_comprobante Is Nothing) OrElse (_comprobante.importe_neto > 0)))
            rules.Add(New SimpleRule("importe_otros_tributos", "El importe de otros tributos debe ser un numero positivo", Function() (_comprobante Is Nothing) OrElse (_comprobante.importe_otros_tributos >= 0)))
            rules.Add(New SimpleRule("importe_operaciones_exentas", "El importe de operaciónes excentas debe ser un numero positivo", Function() (_comprobante Is Nothing) OrElse (_comprobante.importe_operaciones_exentas >= 0)))
            rules.Add(New SimpleRule("moneda", "No hay un tipo de moneda seteado", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.moneda Is Nothing)))
            rules.Add(New SimpleRule("moneda", "La constante asiganda a la moneda no corresponde a un tipo de moneda", AddressOf monedaConstantTypeIsOK))
            rules.Add(New SimpleRule("moneda_cotizacion", "La cotizacion de la moneda debe ser un numero mayor a cero", Function() (_comprobante Is Nothing) OrElse (_comprobante.moneda_cotizacion >= 0)))
            rules.Add(New SimpleRule("items", "No hay una lista de items asociada", Function() (_comprobante Is Nothing) OrElse (Not _comprobante.items Is Nothing)))
            rules.Add(New SimpleRule("items", "No hay items asociados al comprobante", Function() (_comprobante Is Nothing) OrElse (_comprobante.items Is Nothing) OrElse (_comprobante.items.Count > 0)))
            rules.Add(New SimpleRule("items", "Existen items asociados que no son validos", AddressOf itemsAreOk))
            Return rules
        End Function

        Public Function itemsAreOk() As Boolean
            Dim valid As Boolean = True
            If _comprobante Is Nothing OrElse _comprobante.items Is Nothing Then Return valid

            For Each i In items
                Dim bo As New comprobanteItemBO(_daoFactory, i)
                If Not bo.isValid Then
                    valid = False
                    Exit For
                End If
            Next
            Return valid
        End Function

        Private Function tipoComprobanteConstantTypeIsOK() As Boolean
            Dim valid As Boolean
            Try
                valid = _comprobante.tipo_comprobante.categoria = "facturacion" And _comprobante.tipo_comprobante.tipo = "comprobante"
            Catch ex As NullReferenceException
                valid = True
            End Try
            Return valid
        End Function

        Private Function conceptoConstantTypeIsOK() As Boolean
            Dim valid As Boolean
            Try
                valid = _comprobante.concepto.categoria = "facturacion" And _comprobante.concepto.tipo = "concepto"
            Catch ex As Exception
                valid = True
            End Try
            Return valid
        End Function
        Private Function monedaConstantTypeIsOK() As Boolean
            Dim valid As Boolean
            Try
                valid = _comprobante.moneda.categoria = "facturacion" And _comprobante.moneda.tipo = "moneda"
            Catch ex As Exception
                valid = True
            End Try
            Return valid
        End Function
#End Region

        Public ReadOnly Property id As Integer
            Get
                Return _comprobante.id
            End Get
        End Property

        Public Property emisor As empresa
            Get
                Return _comprobante.emisor
            End Get
            Set(ByVal value As empresa)
                _comprobante.emisor = value
                NotifyChanged("emisor")
            End Set
        End Property
        Public Property receptor As entidad
            Get
                Return _comprobante.receptor
            End Get
            Set(ByVal value As entidad)
                _comprobante.receptor = value
                NotifyChanged("receptor")
            End Set
        End Property

        Public Property tipo_comprobante As constante
            Get
                Return _comprobante.tipo_comprobante
            End Get
            Set(ByVal value As constante)
                _comprobante.tipo_comprobante = value
                NotifyChanged("tipo_comprobante")
            End Set
        End Property
        Public Property punto_venta As puntoVenta
            Get
                Return _comprobante.punto_venta
            End Get
            Set(ByVal value As puntoVenta)
                _comprobante.punto_venta = value
                NotifyChanged("punto_venta")
            End Set
        End Property
        Public Overridable Property numero_comprobante As Integer
            Get
                Return _comprobante.numero_comprobante
            End Get
            Set(ByVal value As Integer)
                _comprobante.numero_comprobante = value
                NotifyChanged("numero_comprobante")
            End Set
        End Property
        Public Overridable Property concepto As constante
            Get
                Return _comprobante.concepto
            End Get
            Set(ByVal value As constante)
                _comprobante.concepto = value
                NotifyChanged("concepto")
            End Set
        End Property
        Public Overridable ReadOnly Property importe_total As Double
            Get
                Return _comprobante.importe_total
            End Get
        End Property
        Public Overridable Property importe_total_no_gravados As Double
            Get
                Return _comprobante.importe_total_no_gravados
            End Get
            Set(ByVal value As Double)
                _comprobante.importe_total_no_gravados = value
                NotifyChanged("importe_total_no_gravados")
            End Set
        End Property
        Public Overridable ReadOnly Property importe_neto As Double
            Get
                Return _comprobante.importe_neto
            End Get
        End Property
        Public Overridable ReadOnly Property importe_iva As Double
            Get
                Return _comprobante.importe_iva
            End Get
        End Property
        Public Overridable ReadOnly Property importe_iva21 As Double
            Get
                Return _comprobante.importe_iva21
            End Get
        End Property
        Public Overridable ReadOnly Property importe_iva105 As Double
            Get
                Return _comprobante.importe_iva105
            End Get
        End Property
        Public Overridable Property importe_otros_tributos As Double
            Get
                Return _comprobante.importe_otros_tributos
            End Get
            Set(ByVal value As Double)
                _comprobante.importe_otros_tributos = value
                NotifyChanged("importe_otros_tributos")
            End Set
        End Property
        Public Overridable Property importe_operaciones_exentas As Double
            Get
                Return _comprobante.importe_operaciones_exentas
            End Get
            Set(ByVal value As Double)
                _comprobante.importe_operaciones_exentas = value
                NotifyChanged("importe_operaciones_exentas")
            End Set
        End Property
        Public Overridable Property moneda As constante
            Get
                Return _comprobante.moneda
            End Get
            Set(ByVal value As constante)
                _comprobante.moneda = value
                NotifyChanged("moneda")
            End Set
        End Property
        Public Overridable Property moneda_cotizacion As Double
            Get
                Return _comprobante.moneda_cotizacion
            End Get
            Set(ByVal value As Double)
                _comprobante.moneda_cotizacion = value
                NotifyChanged("moneda_cotizacion")
            End Set
        End Property
        Public Overridable Property fecha_comprobante As Date
            Get
                Return _comprobante.fecha_comprobante
            End Get
            Set(ByVal value As Date)
                _comprobante.fecha_comprobante = value
                NotifyChanged("fecha_comprobante")
            End Set
        End Property
        Public Overridable Property fecha_vto_pago As Date
            Get
                Return _comprobante.fecha_vto_pago
            End Get
            Set(ByVal value As Date)
                _comprobante.fecha_vto_pago = value
                NotifyChanged("fecha_vto_pago")
            End Set
        End Property
        Public Overridable Property fecha_srv_desde As Date
            Get
                Return _comprobante.fecha_srv_desde
            End Get
            Set(ByVal value As Date)
                _comprobante.fecha_srv_desde = value
                NotifyChanged("fecha_srv_desde")
            End Set
        End Property

        Public Overridable Property fecha_srv_hasta As Date
            Get
                Return _comprobante.fecha_srv_hasta
            End Get
            Set(ByVal value As Date)
                _comprobante.fecha_srv_hasta = value
                NotifyChanged("fecha_srv_hasta")
            End Set
        End Property
        Public Overridable Property cae As Integer
            Get
                Return _comprobante.cae
            End Get
            Set(ByVal value As Integer)
                _comprobante.cae = value
                NotifyChanged("cae")
            End Set
        End Property
        Public Overridable Property items As ISet(Of comprobanteItem)
            Get
                Return _comprobante.items
            End Get
            Set(ByVal value As ISet(Of comprobanteItem))
                _comprobante.items = value
                NotifyChanged("items")
            End Set
        End Property
        Public Sub setItems(ByRef itemList As IList(Of comprobanteItem))
            _comprobante.items.Clear()
            For Each item In itemList
                addItem(item)
            Next
            NotifyChanged("items")
        End Sub
        Public Overridable Sub addItem(ByRef theItem As comprobanteItem)
            _comprobante.addItem(theItem)
            NotifyChanged("items")
        End Sub
        Public Overridable Property comprobantes_asociados As ISet(Of comprobante)
            Get
                Return _comprobante.comprobantes_asociados
            End Get
            Set(ByVal value As ISet(Of comprobante))
                _comprobante.comprobantes_asociados = value
                NotifyChanged("comprobantes_asociados")
            End Set
        End Property
        Public Overridable Sub addComprobanteAsociado(ByRef theComprobanteAsociado As comprobanteItem)
            _comprobante.addComprobanteAsociado(theComprobanteAsociado)
            NotifyChanged("comprobantes_asociados")
        End Sub
        Public Property actualizarStockautomatico As Boolean
            Get
                Return _actualizarStockAutomatico
            End Get
            Set(ByVal value As Boolean)
                _actualizarStockAutomatico = value
            End Set
        End Property

        Private Sub actualizarStock(ByRef stock As stock, ByVal cantidad As Double)
            stock.cantidad = stock.cantidad + cantidad * factorAfeccionDeStock()
        End Sub
        Private Function factorAfeccionDeStock() As Integer
            Dim factor As Integer = 0
            Select Case _comprobante.tipo_comprobante.codigo
                Case 1, 6, 11
                    factor = -1
                Case Else
                    factor = 0
            End Select
            Return IIf(_actualizarStockAutomatico, factor, 0)
        End Function
    End Class
End Namespace